<?php
/**
 * 图片处理模型
 * User: wuhao
 * Date: 2017/3/13
 * Time: 14:00
 */
namespace app\backend\model;
use think\model;
class Photo extends model
{
    private $_photoTable = 'z_photo';

    public function __construct() {
        parent::__construct();
    }

    /**
     * 增加图片
     * @date   2017-3-13
     * @author wuhao
     *
     * @param int     $rid    对应类型id,(楼宇id,房源id,租赁合约id等)
     * @param int     $rtype  类型：1=楼宇 2=房源 3=租赁签约附件 4=用户
     * @param int     $srtype 类型对应的子类
     * @param string  $url    图片地址
     * @param int     $user   添加人id
     * @return
     */
    public function addPhoto($rid, $rtype, $srtype, $url, $user)
    {
        if (!$rid || !in_array($rtype, [1, 2, 3, 4]) || !$srtype ||!$url ||!$user) {
            return false;
        }
        $data = [
            'rid'         => $rid,
            'rtype'       => $rtype,
            'srtype'      => $srtype,
            'url'         => $url,
            'create_user' => $user,
            'create_time' => time(),
        ];
        return $this->setTable($this->_photoTable)->insertGetId($data);
    }

    /**
     * 批量插入图片增加图片
     * @date   2017-3-13
     * @author wuhao
     *
     * @param array     $array    图片信息数组
     * @return
     */
    public function addAll(array $array)
    {
        if (!$array || !is_array($array)) {
            return false;
        }
        return $this->setTable($this->_photoTable)->insertAll($array);
    }

    /**
     * 删除某种类型id对应的图片
     * @date   2017-3-13
     * @author wuhao
     *
     * @param int     $rid    对应类型id,(楼宇id,房源id,租赁合约id等)
     * @param int     $rtype  类型：1=楼宇 2=房源 3=租赁签约 4=用户
     * @param int     $srtype 类型对应的小类
     * @return
     */
    public function deletePhoto($rid, $rtype, $srtype = 1)
    {
        if (!is_numeric($rid) || !is_numeric($rtype) || !is_numeric($srtype)) {
            return false;
        }
        $where = [
            'rid'    => $rid,
            'rtype'  => $rtype,
            'srtype' => $srtype,
        ];
        return $this->setTable($this->_photoTable)->where($where)->delete();
    }

    /**
     * 获取图片信息
     * @date   2017-3-13
     * @author wuhao
     *
     * @param array|string   $where    条件
     * @param string         $field    字段
     * @return
     */
    public function getPhoto($where, $field = '*')
    {
        if (!$where) {
            return false;
        }
        return $this->setTable($this->_photoTable)->field($field)->where($where)->select();
    }
}